package com.tree.common.interceptor;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName com.demo.common.interceptor
 * Description: <类功能描述>. <br>
 * <p>
 * <使用说明>
 * </p>
 * @Author tree
 * @Date 2019-05-25 23:17
 * @Version 1.0
 */
public class BaseInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(BaseInterceptor.class);

    static final String JSON = "application/json;charset=UTF-8";

    void writeResponse(HttpServletRequest request, HttpServletResponse response,Object o) throws Exception{
        ServletServerHttpRequest serverHttpRequest = new ServletServerHttpRequest(request);
        response.setHeader("Access-Control-Allow-Orign", serverHttpRequest.getHeaders().getOrigin());
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Heads", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        //支持Http1.1
        response.setHeader("Cache-Control", "no-cache, no-store,must-revalidate");

        Object json = JSONObject.toJSON(o);
        response.getWriter().append(json.toString()).flush();
        response.getWriter().close();
    }

}
